<template>
  <el-dialog :title="`${employeeData.id?'查看':'新增'}员工`" :visible="showDialog" @close="btnClear">
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :model="employeeData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeData.username" style="width:90%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employeeData.mobile" style="width:90%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employeeData.timeOfEntry" style="width:90%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeData.formOfEmployment" style="width:90%" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeData.workNumber" style="width:90%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="employeeData.departmentName" style="width:90%" placeholder="请选择部门" @focus="isShowTree = true" />
        <!--
          default-expand-all ==> 是否打开所有的子级
          data ==> 要渲染的数据
          props ==> 将默认显示的内容(label:'值')转换为自己想要的key(例：name:'值')
         -->
        <el-tree v-if="isShowTree" :default-expand-all="true" :data="depts" :props="{label:'name'}" class="departmentTree" @node-click="nodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employeeData.correctionTime" style="width:90%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnClear">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employee'
import { getDepartmentList } from '@/api/department'
import { listToTreeData } from '@/utils'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      employeeData: {
        username: '', // 姓名
        workNumber: '', // 工号
        formOfEmployment: '', // 聘用形式
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        mobile: '', // 手机号
        correctionTime: ''// 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
            min: 1, max: 20, message: '用户姓名为1-20位'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }, {
            pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          // blur 是立马执行，而change 是赋值完之后在执行
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' }
        ]
      },
      EmployeeEnum: EmployeeEnum.hireType, // 枚举
      depts: [], // 部门数据
      isShowTree: false // 显示/隐藏树型
    }
  },
  async created() {
    this.getDepartmentList()
  },
  methods: {
    // 显示/隐藏树型
    nodeClick(nodeData) {
      // console.log('树型内容', nodeData)
      // 将点击的内容交给 employeeData.departmentName
      this.employeeData.departmentName = nodeData.name
      // 同时关闭树型
      this.isShowTree = false
    },
    async getDepartmentList() {
      const { depts } = await getDepartmentList()
      // 将封装好的树形结构传入对应的数据，根据父级找自己的子级来生成树形结构，最后在渲染到页面中
      this.depts = listToTreeData(depts, '')
    // console.log('获取部门信息', await getDepartmentList())
    },
    // 关闭
    async btnClear() {
      this.employeeData = {
        username: '', // 姓名
        workNumber: '', // 工号
        formOfEmployment: '', // 聘用形式
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        mobile: '', // 手机号
        correctionTime: ''// 转正时间
      }
      await this.$refs.formData.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 确定
    async btnOk() {
      // console.log('确定')
      await this.$refs.formData.validate()
      // 新增
      await addEmployee(this.employeeData)
      this.$message.success('新增成功')
      this.$emit('update:showDialog', false)
      this.$emit('addEmployee')
    }
  }
}
</script>

<style scodep>
.departmentTree{
  position:absolute;
  width: 90%;
  height:180px;
  border: 1px solid #999;
  overflow:auto;
  z-index:999;
}
</style>

